package com.heer.gymnasium.module.order.views;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.api.constants.IConstants;
import com.heer.gymnasium.base.BaseActivity;
import com.heer.gymnasium.constant.Type;
import com.heer.gymnasium.module.card.beans.CardInfoResponseBean;
import com.heer.gymnasium.module.card.views.MyCardActivity;
import com.heer.gymnasium.module.main.views.MainActivity;
import com.heer.gymnasium.module.order.beans.MyOrderBean;
import com.heer.gymnasium.module.order.beans.OrderBean;
import com.heer.gymnasium.module.order.beans.OrderInfoResponseBean;
import com.heer.gymnasium.util.StringUtils;
import com.heer.gymnasium.util.WxShareUtils;
import com.heer.imagepick.DensityUtils;
import com.zbar.lib.encoding.EncodingUtils;

import java.text.SimpleDateFormat;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yan
 * on 2018/1/26
 * 支付成功
 */

public class PaidSuccessActivity extends BaseActivity {
    private static final int LAYOUT = R.layout.layout_pay_sucsses;
    private static final int MYCARD_ID = R.id.layout_pay_sucsses_cardbag_tv;
    private static final int COMPLATE_ID = R.id.layout_pay_sucsses_complate_tv;
    private static final int RES_ID_PROMPT_INFO = R.id.layout_pay_sucsses_tv;
    private static final int RES_ID_DESCRIPTION = R.id.layout_pay_sucsses_description_tv;

    private static final int RES_ID_SPECIAL_LL = R.id.layout_pay_sucsses_special_ll;
    private static final int RES_ID_SPECIAL_TIME = R.id.layout_pay_sucsses_time_tv;
    private static final int RES_ID_SPECIAL_ADDRESS = R.id.layout_pay_sucsses_address_tv;
    private static final int RES_ID_SPECIAL_SPACE = R.id.layout_pay_sucsses_space_tv;
    private static final int RES_ID_SPECIAL_IPHONE = R.id.layout_ticker_detail_iphone_iv;
    private static final int RES_ID_SPECIAL_QRCODE = R.id.layout_pay_sucsses_qrcode_iv;

    //    页面跳转
    public static void startActivity(Activity context) {
        Intent intent = new Intent(context, PaidSuccessActivity.class);
        context.startActivity(intent);
    }

    public static void startActivity(Activity context, int type) {
        Intent intent = new Intent(context, PaidSuccessActivity.class);
        intent.putExtra(EXTRA_TYPE, type);
        context.startActivity(intent);
    }

    @BindView(RES_ID_PROMPT_INFO)
    TextView textView;
    @BindView(RES_ID_SPECIAL_QRCODE)
    ImageView qrImageView;
    private PaidSuccessActivity $this;

    @BindView(RES_ID_SPECIAL_LL)
    LinearLayout llSpecial;
    @BindView(RES_ID_SPECIAL_TIME)
    TextView tvTime;
    @BindView(RES_ID_SPECIAL_ADDRESS)
    TextView tvAddress;
    @BindView(RES_ID_SPECIAL_SPACE)
    TextView tvSpace;
    @BindView(RES_ID_SPECIAL_IPHONE)
    TextView tvIphone;
    @BindView(MYCARD_ID)
    TextView tvMyCard;
    private String orderType = "";
    private String detailId = "";

    @Override
    protected void init() {
        super.init();
        setContentView(LAYOUT);
        $this = this;
    }

    @Override
    protected void initView() {
        super.initView();
        int type = getIntent().getIntExtra(EXTRA_TYPE, 0);
        titleUtils.startMainActivity(this);
        if (type == 1) {
            textView.setText("领取成功");
        }
        orderType = IConstants.orderType;
        if (!TextUtils.isEmpty(orderType) && !TextUtils.isEmpty(IConstants.orderId)) {
            if (orderType.equals(Type.ORDER_ONE_STDIUM) || orderType.equals(Type.ORDER_MORE_STDIUM)
                    || (orderType.equals(Type.ORDER_CARD))) {
                llSpecial.setVisibility(View.VISIBLE);
                getOrderInfo(IConstants.orderId);
                if (!orderType.equals(Type.ORDER_CARD)) {
                    tvMyCard.setText("分享订场券");
                }
            }
        }
    }

    @OnClick({MYCARD_ID, COMPLATE_ID})
    public void onClick(View view) {
        finish();
        switch (view.getId()) {
            case MYCARD_ID:
                if ( orderType.equals(Type.ORDER_ONE_STDIUM) || orderType.equals(Type.ORDER_MORE_STDIUM)) {
                    if (TextUtils.isEmpty(detailId)) {
                        return;
                    }
                    WxShareUtils wxShareUtils = new WxShareUtils($this);
                    wxShareUtils.wxShare(detailId, Type.SHARE_COUPON);
                } else {
                    MyCardActivity.startActivity(this);
                }
                break;
            case COMPLATE_ID:
                MainActivity.startActivity(mContext, 0);
                break;
        }
    }


    private void getOrderInfo(String id) {
        IConstants.orderId = "";
        IConstants.orderType = "";
        mAppHelper.apiService().getOrderInfo(id).enqueue(new BaseCallback<ApiResponse<OrderInfoResponseBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<OrderInfoResponseBean>> call, Response<ApiResponse<OrderInfoResponseBean>> response) {

                if (orderType.equals(Type.ORDER_ONE_STDIUM) || orderType.equals(Type.ORDER_MORE_STDIUM)) {
                    final OrderBean bean = OrderBean.dataChange(response.body().getData(), Type.ORDER_MORE_STDIUM);
                    if (bean.getDetailList() != null) {
                        detailId = ((MyOrderBean.DetialStadium) bean.getDetailList().get(0)).getDetailId();
                        getMyCardInfo(detailId);
                    }
                } else if (orderType.equals(Type.ORDER_CARD)) {
                    final OrderBean bean = OrderBean.dataChange(response.body().getData(), Type.BUY_CARD_TYPE);
                    if (bean.getCardDetail() != null && !TextUtils.isEmpty(bean.getCardDetail().getCardId())) {
                        getMyCardInfo(bean.getCardDetail().getCardId());
                    }
                }
            }
        });
    }

    //获得我的卡详情
    private void getMyCardInfo(String id) {

        mAppHelper.apiService().getMyCardInfo(id).enqueue(new BaseCallback<ApiResponse<CardInfoResponseBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<CardInfoResponseBean>> call, Response<ApiResponse<CardInfoResponseBean>> response) {
                CardInfoResponseBean bean = response.body().getData();
                fillData(bean);
            }
        });

    }

    //填充数据
    private void fillData(CardInfoResponseBean bean) {
        if (orderType.equals(Type.ORDER_ONE_STDIUM) || orderType.equals(Type.ORDER_MORE_STDIUM)) {
            String sTime = StringUtils.getPhpDateFormat(bean.getStime(), new SimpleDateFormat("HH:mm"));
            String eTime = StringUtils.getPhpDateFormat(bean.getEtime(), new SimpleDateFormat("HH:mm"));
            String date = StringUtils.getPhpDateFormat(bean.getEtime(), new SimpleDateFormat("yyyy/MM/dd"));
            tvTime.setText(StringUtils.getWeekOfDate(bean.getEtime()) + "(" + date + ")" + "\n" + sTime + "-" + eTime);
        } else {
            String date = StringUtils.getPhpDateFormat(bean.getEtime(), new SimpleDateFormat("yyyy/MM/dd"));
            tvTime.setText(StringUtils.getWeekOfDate(bean.getEtime()) + "(" + date + ")");
        }
        StringBuffer addressCode = new StringBuffer();
        for (int i = 0; i < bean.getAddress().length; i++) {
            addressCode.append(bean.getAddress()[i] + "\n");
        }
        tvAddress.setText(addressCode.substring(0, addressCode.toString().lastIndexOf("\n")).trim());

        StringBuffer placeCode = new StringBuffer();
        for (int i = 0; i < bean.getPlace().length; i++) {
            placeCode.append(bean.getPlace()[i] + "\n");
        }
        if (!placeCode.toString().trim().equals("null")) {
            tvSpace.setText(placeCode.substring(0, placeCode.toString().lastIndexOf("\n")));
        }
        StringBuffer iphoneCode = new StringBuffer();
        for (int i = 0; i < bean.getPhone().length; i++) {
            iphoneCode.append(bean.getPhone()[i] + "\n");
        }
        tvIphone.setText(iphoneCode);
        qrImageView.setImageBitmap(EncodingUtils.createQRCode(bean.getEwm(), DensityUtils.dip2px($this, 80),
                DensityUtils.dip2px($this, 80),
                BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)));
    }
}
